package com.xingongchang.baolebei.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.xingongchang.baolebei.BaolebeiApp;
import com.xingongchang.baolebei.R;
import com.xingongchang.baolebei.activity.TanDatilactivity;
import com.xingongchang.baolebei.activity.TopicDetailActivity;
import com.xingongchang.baolebei.protocol.TOPIC;

public class TopicAdapter extends BaseAdapter {

	public ArrayList<TOPIC> list = new ArrayList<TOPIC>();
	private LayoutInflater inflater;
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	Context mcontext;

	public TopicAdapter(Context context, ArrayList<TOPIC> list) {
		this.list = list;
		inflater = LayoutInflater.from(context);
		this.mcontext=context;
		imageLoader.init(ImageLoaderConfiguration.createDefault(mcontext)); 
	}

	@Override
	public int getCount() {

		return list.size();

	}

	@Override
	public Object getItem(int position) {

		return list.get(position);
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.forum_cell, null);
			holder.Topic_detailed = (FrameLayout) convertView
					.findViewById(R.id.Topic_detailed);
			holder.topic_img = (ImageView) convertView
					.findViewById(R.id.topic_img);
			holder.topic_tag = (TextView) convertView
					.findViewById(R.id.topic_tag);
			holder.topic_title = (TextView) convertView
					.findViewById(R.id.topic_title);
			holder.topic_username = (TextView) convertView
					.findViewById(R.id.topic_username);
			holder.topic_replycount = (TextView) convertView
					.findViewById(R.id.topic_replycount);
			holder.topic_date = (TextView) convertView
					.findViewById(R.id.topic_date);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final TOPIC topic = list.get(position);
		holder.Topic_detailed.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(mcontext,TopicDetailActivity.class);
				intent.putExtra("topicid", topic.topicid);
				mcontext.startActivity(intent);
			}
		});
		
		if (topic.is_top == 1) {
			holder.topic_tag.setVisibility(View.VISIBLE);
			holder.topic_title.setText("\t\t\t\t\t"+topic.title);
		} else {
			holder.topic_tag.setVisibility(View.GONE);
			holder.topic_title.setText(topic.title);
		}
	
		if (topic.imgurl.length > 0 && topic.imgurl != null){
			String path = topic.imgurl[0];
			Log.i("Api", path);
			holder.topic_img.setVisibility(View.VISIBLE);
			imageLoader.displayImage(topic.imgurl[0], holder.topic_img,BaolebeiApp.options);
		}else{
			holder.topic_img.setVisibility(View.GONE);
		}
		holder.topic_username.setText(topic.user_name);
		holder.topic_replycount.setText(topic.replycount);
		Date date = getDate(topic.creaton);
		holder.topic_date.setText(toString(date));
		return convertView;
	}
	//将日期格式字符串转换成日期格式
	public static Date getDate(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date s = null;
		try {
			s = sdf.parse(date);
		} catch (Exception e) {
			System.out.println("日期转换错误" + e);
		}
		return s;
	}

	//将日期格式转换成日期格式字符串
	public static String toString(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
		return sdf.format(date);
	}

	class ViewHolder {
        private FrameLayout Topic_detailed;
		private ImageView topic_img;
		private TextView topic_tag;
		private TextView topic_title;
		private TextView topic_username;
		private TextView topic_date;
		private TextView topic_replycount;

	}

}
